
<template>
    <el-form 
    
    ref="form" 
    :inline="true" 
    :model="form" 
    
    label-width="100px" 
    enctype="multipart/form-data" >
        
     

        <el-form-item 
        label="案例主标题"
       
         >
            <el-input  v-model="form.firstTitle" ></el-input>
        </el-form-item>
      

        <el-input
        type="textarea"
        :rows="2"
        placeholder="案例内容"
        v-model="form.secondTitle">
        </el-input>

        <el-form-item 
        label="浏览次数"
       
         >
            <el-input  v-model="form.hit" ></el-input>
        </el-form-item>
        <el-form-item 
        label="收藏次数"
       
         >
            <el-input  v-model="form.shoucang" ></el-input>
        </el-form-item>
        <el-form-item 
        label="喜欢次数"
       
         >
            <el-input  v-model="form.like" ></el-input>
        </el-form-item>


        <el-form-item label="首页推荐">
            <el-switch 
            v-model="form.toIndex"
            active-value="1"
            inactive-value="0"></el-switch>
        </el-form-item>
        <div class='picUp'>
            <el-form-item label="首页用图">
                <el-upload
                ref="uploadtop"
                action="https://jsonplaceholder.typicode.com/posts/"
                list-type="picture-card"
                :on-preview="handlePictureCardPreview"
                :on-remove="handleRemove"
                :before-upload="beforeuploadtop"
                :multiple="true"
                :auto-upload="false"
                enctype="multipart/form-data"
                :file-list="fileList">
                <i class="el-icon-plus"></i>
                </el-upload>
                <el-dialog :visible.sync="dialogVisible" >
                <img width="100%" :src="dialogImageUrl" alt="">
                </el-dialog>
            </el-form-item>
            
        </div>
        
        
        
        <!-- <Editor @getEditorData="getEditorData" :url='url'></Editor> -->


        <el-form-item>
            <el-button type="primary" @click="onSubmit">立即创建</el-button>
            <el-button>取消</el-button>
        </el-form-item>
    </el-form>
</template>


<script>
import axios from 'axios'
import { mapState,mapMutations } from 'vuex'
import Editor from 'index/components/Editor'
export default {
    name:'ProcutForm',  
    components:{
        Editor 
    },
    data() {
        return {
            
            editorData: '',
            
            form: {
                
                firstTitle: '',
                secondTitle: '',
                hit:0,
                shoucang:0,
                like:0,
              
                toIndex:0,
              
                
            },
            formData: new FormData(),
            dialogImageUrl: '',
            dialogVisible: false,
            fileList:[],
            i:1
        }
    },
    mounted() {
        // console.log('form')
        this.init();
    },
    
    computed:{
        ...mapState(['baseUrl','infoChange']),
        url:function (){
            return this.baseUrl+"cms/news/content"
        }
    },
    methods: {
      init(){
        
      },  
    
      
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
     
        getEditorData(val) {

           this.editorData = val 
        //    console.log(this.editorData)

        } ,
      
        //阻止upload的自己上传，进行再操作
        beforeuploadtop(file) {
            // console.log(file);
            //创建临时的路径来展示图片
            var windowURL = window.URL || window.webkitURL;
            
            this.dialogImageUrl = windowURL.createObjectURL(file);
            
           
           
            this.formData.append('top[]', file)
           
            
            return false;
        },
        //覆盖默认的上传行为
        httprequest() {

        },
        onSubmit() {
             let that = this
            
            // console.log(param)
          
           
            var toIndex = this.form.toIndex;
            // var content =this.editorData; 
           
            var firstTitle =this.form.firstTitle;
            var secondTitle =this.form.secondTitle;
            var hit =this.form.hit ;
            var shoucang =this.form.shoucang ;
            var like =this.form.like ;
          
          
            //下面append的东西就会到form表单数据的fields中；
          
            
            that.formData.append('toIndex', toIndex);
            // that.formData.append('summary', content);
          
            that.formData.append('firstTitle', firstTitle);
            that.formData.append('secondTitle',secondTitle);
            that.formData.append('hit',hit);
            that.formData.append('shoucang',shoucang);
            that.formData.append('like',like);
             
            
            // console.log(that.formData)
            let config = {
                headers: {
                    'Content-Type': 'multipart/form-data'
                    
                },
                
                
            }
          
            that.$refs.uploadtop.submit();
            // console.log(this.formData.getAll("file"))
            axios.post(this.baseUrl+"wx/case/addCase", this.formData, config).then(function(result) {
                            // console.log(result);
                            // alert(result.data);

                            

                            if(result.data.errno===0){
                                that.$alert('新增成功', '特别提示', {
                                confirmButtonText: '确定',
                                closeOnClickModal:true,
                                center:true,
                                callback: action => {
                                    that.$router.push('/wechat/NewsList');
                                    that.form={brand_right:0}
                                    
                                    that.changeActive('/wechat/NewsList') 
                                    that.changeInfo(true) 
                                    that.formData.delete('file[]');
                                    that.$message({
                                    message: '恭喜你，这是一条成功消息',
                                    type: 'success',
                                    duration:'3000',
                                    showClose:true,
                                    });
                                }
                            });
                                
                            }

                            
            })
            that.$refs.uploadtop.clearFiles();
            
        },
        ...mapMutations(['changeInfo','changeActive']),
        handleRemove(file, fileList) {
        // console.log(file, fileList);
      },
      handlePictureCardPreview(file) {
        // this.dialogImageUrl = file.url;
        this.dialogVisible = true;
        // console.log(handlePictureCardPreview);
      }
       
    }
}
</script>
<style lang="stylus" >
.el-message-box__wrapper
    z-index 20003!important
    .addType
        display inline-block
        cursor pointer
</style>
